import { createSlice } from '@reduxjs/toolkit'

const counterStore = createSlice({
	name: 'counter',

	// 初始化状态
	initialState: {
		count: 0,
	},

	// 定义reducers [同步方法]
	reducers: {
		increment(state) {
			state.count += 1
		},
		decrement(state) {
			state.count -= 1
		},
		addToNum(state, action) {
			state.count += action.payload
		},
	},
})

// 解构 actionCreater 函数
const { increment, decrement, addToNum } = counterStore.actions
// 获取 reducer
const counterReducer = counterStore.reducer

// 按需导出 actionCreater
export { increment, decrement, addToNum }
// 默认导出 reducer
export default counterReducer
